// pages/Myorder/index.js
const {
  getOrder,
  changeOrderStatus
} = require('../../request/api.js');
const App = getApp();

Page({
  data: {
    orderList: [],
    moreShow: false,
    infoShow: false,
    items: [{
        label: '分享',
        value: 1
      },
      {
        label: '已回收',
        value: 2
      },
      {
        label: '下架',
        value: 3
      }
    ],

    itemDetail: {}, //当前选中订单
    // 菲渲染数据；
    pageIndex: 1,
    pageSize: 20,
    pageTotal: 1,
    selectedId: '', //当前选中id；
  },
  onLoad: function (options) {
    this.getOrder()
  },
  // onLoad: function () {
  //   wx.showShareMenu({
  //     withShareTicket: true
  //   })
  // }
  // 分享；
  onShareAppMessage() {
  },
  onReachBottom: function () {
    this.getOrder(this.data.pageIndex + 1)
  },
  async getOrder(pageIndex = 1) {
    if (pageIndex > this.data.pageTotal) return;
    // {
    //   pageIndex: pageIndex,
    //   pageSize: this.data.pageSize
    // }
    // const {
    //   rows
    // }
    let uid = wx.getStorageSync('userId'); 
    let data = await getOrder({
      releaseWechatUid: uid
    })
    let dataRows =[]
    dataRows=data.items
    if (dataRows) {
      dataRows.forEach(item => {
        item.filePath = App.globalData.imgBase + item.recoveryImgPath
        const formattedRecoveryDoortime = this.formatTime(new Date(item.recoveryDoortime).getTime());  
        // console.log(formattedRecoveryDoortime);  
        item.recoveryDoortime = formattedRecoveryDoortime
      })
      this.setData({
        orderList: dataRows
      })
    }
  },
  formatTime(timeStr){//格式化时间
    const date = new Date(timeStr);  
  return `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
  },
  showMore(e) {
    console.log(e);
    let id = e.currentTarget.dataset.index;
    this.setData({
      moreShow: true,
      selectedId: id
    })
  },
  hideMore(e) {
    this.setData({
      moreShow: false
    })
  },

  async showInfo(e) {
    let index = e.currentTarget.dataset.index;
    // const data = await getDetail({id:id});
    this.setData({
      infoShow: true,
      itemDetail: this.data.orderList[index]
    })
  },
  hideInfo() {
    this.setData({
      infoShow: false
    })
  },
  // 修改订单状态；
  async changeOrderStatus(value = 3) {
    await changeOrderStatus({
      id: this.data.selectedId,
      recoveryStatus: value
    })
    wx.showToast({
      title: '修改成功！'
    })
    this.getOrder()
  },
  // 已回收
  retrieve() {
    // 回收
    let _this = this;
    wx.showModal({
      content: '是否确认回收?',
      complete: (res) => {
        if (res.confirm) {
          _this.hideMore();
          _this.changeOrderStatus(1);
        }
      }
    })
  },
  // 下架
  underShelf() {
    // 下架
    let _this = this;
    wx.showModal({
      title: '操作后,将在市集下架本条发布信息,是否继续?',
      // content: '是否确认下架?',
      complete: (res) => {
        if (res.confirm) {
          _this.hideMore();
          _this.changeOrderStatus(3);
        }
      }
    })
  }
})